package com.mandala;

import java.util.ArrayList;
import java.util.List;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.context.request.async.DeferredResult;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.builders.ResponseMessageBuilder;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ResponseMessage;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
 
@Configuration
@EnableSwagger2
public class Swagger2 {
	
    
//swagger2的配置文件，这里可以配置swagger2的一些基本的内容，比如扫描的包等等
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
        		.genericModelSubstitutes(DeferredResult.class)
				.useDefaultResponseMessages(false)
				.globalResponseMessage(RequestMethod.GET,customerResponseMessage())
                .globalResponseMessage(RequestMethod.POST,customerResponseMessage())
                .globalResponseMessage(RequestMethod.DELETE,customerResponseMessage())
				.forCodeGeneration(true)
				.pathMapping("/")
				.select()// 选择那些路径和api会生成document
				.apis(RequestHandlerSelectors.basePackage("com.mandala.controller"))//指定提供接口所在的基包
				.paths(PathSelectors.any())// 对所有路径进行监控
				.build()
				.apiInfo(apiInfo());
                /*.apiInfo(apiInfo())
                .select()
                //为当前包路径
                .apis(RequestHandlerSelectors.basePackage("com.mandala.controller"))
                .paths(PathSelectors.any())// 对所有路径进行监控
                .build();*/
    }
    
	private List<ResponseMessage> customerResponseMessage() {
		//自定义异常信息
        ArrayList<ResponseMessage> responseMessages = new ArrayList<ResponseMessage>() {{
               add(new ResponseMessageBuilder().code(200).message("成功").build());
               add(new ResponseMessageBuilder().code(400).message("请求参数错误").responseModel(new ModelRef("Error")).build());
               add(new ResponseMessageBuilder().code(401).message("权限认证失败").responseModel(new ModelRef("Error")).build());
               add(new ResponseMessageBuilder().code(403).message("请求资源不可用").responseModel(new ModelRef("Error")).build());
               add(new ResponseMessageBuilder().code(404).message("请求资源不存在").responseModel(new ModelRef("Error")).build());
               add(new ResponseMessageBuilder().code(409).message("请求资源冲突").responseModel(new ModelRef("Error")).build());
               add(new ResponseMessageBuilder().code(415).message("请求格式错误").responseModel(new ModelRef("Error")).build());
               add(new ResponseMessageBuilder().code(423).message("请求资源被锁定").responseModel(new ModelRef("Error")).build());
               add(new ResponseMessageBuilder().code(500).message("服务器内部错误").responseModel(new ModelRef("Error")).build());
               add(new ResponseMessageBuilder().code(501).message("请求方法不存在").responseModel(new ModelRef("Error")).build());
               add(new ResponseMessageBuilder().code(503).message("服务暂时不可用").responseModel(new ModelRef("Error")).build());
               add(new ResponseMessageBuilder().code(-1).message("未知异常").responseModel(new ModelRef("Error")).build());
           }};

           return responseMessages;

}

	//构建 api文档的详细信息函数,注意这里的注解引用的是哪个
    private ApiInfo apiInfo() {
    	
        return new ApiInfoBuilder()
                //页面标题
                .title("portal系统数据接口文档")
                //创建人
                //.contact(new Contact("HUACHAO", "http://www.baidu.com", ""))
                //.termsOfServiceUrl("http://www.baidu.com") 
                //创建人 
                //.contact("张亮")
                //版本号
                .version("1.0")
                //描述
                .description("API 描述")
                .build();
    }
 
 
}
